package com.hongyu.smart.village.ui.activity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.google.android.material.navigation.NavigationView;
import com.hongyu.smart.village.R;
import com.hongyu.smart.village.config.Constant;
import com.hongyu.smart.village.entity.TabEntity;
import com.hongyu.smart.village.event.Event;
import com.hongyu.smart.village.ui.adapter.FragmentViewPagerAdapter;
import com.hongyu.smart.village.ui.base.BaseActivity;
import com.hongyu.smart.village.ui.fragment.HomeFragment;
import com.hongyu.smart.village.ui.fragment.MyFragment;
import com.hongyu.smart.village.ui.widget.ToastTool;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.BindView;

public class HomeActivity extends BaseActivity {

    ActionBarDrawerToggle mToggle;
    @BindView(R.id.drawerlayout)
    DrawerLayout drawerLayout;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.navigationview)
    NavigationView navigationView;

    @BindView(R.id.tab_layout)
    CommonTabLayout tabLayout;
    @BindView(R.id.viewpager)
    ViewPager viewPager;
//    @BindView(R.id.iv_avator)
//    ImageView ivAvator;
//    @BindView(R.id.tv_author)
//    TextView tvAvator;
//    @BindView(R.id.tv_name)
//    TextView tvName;

    int index=0,pagenum=-1,newPosition=-1;

    ArrayList<Fragment> fragments=new ArrayList<>();
    ArrayList<String> titles=new ArrayList<>();
    ArrayList<CustomTabEntity> tabEntities=new ArrayList<>();
    FragmentViewPagerAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initActivityData() {
        initViewData();


    }

    private void initViewData() {
//        tvName.setText(SPUtil.getRealName());
    }

    @Override
    protected void initContentView() {
        toolbar.inflateMenu(R.menu.toolbar_menu);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                int menuItemId = item.getItemId();
                ToastTool.show(""+menuItemId);
                return true;
            }
        });



        mToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open,R.string.close){
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);

            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);

            }
        };

        mToggle.syncState();
        drawerLayout.setDrawerListener(mToggle);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.one:
                        Toast.makeText(getApplicationContext(),"one",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.two:
                        Toast.makeText(getApplicationContext(),"two",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.three:
                        Toast.makeText(getApplicationContext(),"three",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.four:
                        Toast.makeText(getApplicationContext(),"four",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.five:
                        Toast.makeText(getApplicationContext(),"five",Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.six:
                        Toast.makeText(getApplicationContext(),"six",Toast.LENGTH_SHORT).show();
                        break;
                }
                return true;
            }
        });
        initViewPager();


    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        getMenuInflater().inflate(R.menu.toolbar_menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                drawerLayout.openDrawer(GravityCompat.START);
                break;

            default:
        }
        return true;
    }

    private void initViewPager() {
        HomeFragment homeFragment=new HomeFragment();
        MyFragment myFragment=new MyFragment();
        fragments.add(homeFragment);
        fragments.add(myFragment);
        adapter=new FragmentViewPagerAdapter(getSupportFragmentManager(),titles,fragments);
        viewPager.setAdapter(adapter);
        tabEntities.add(new TabEntity("首页",R.drawable.ic_baseline_home_24_on,R.drawable.ic_baseline_home_24));
        tabEntities.add(new TabEntity("我的",R.drawable.ic_baseline_text_snippet_24_on,R.drawable.ic_baseline_text_snippet_24));
        titles.add("首页");
        titles.add("我的");

        tabLayout.setTabData(tabEntities);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if(pagenum==position){
                    refreshPageData();

                }else {
                    pagenum=position;
                    tabLayout.setCurrentTab(position);

                    EventBus.getDefault().post(new Event<>(Constant.FRAGMENT_SELECT_CHANGE_EVENT, titles.get(position)));
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {


                tabLayout.setCurrentTab(position);
                viewPager.setCurrentItem(position);

                EventBus.getDefault().post(new Event<>(Constant.FRAGMENT_SELECT_CHANGE_EVENT, titles.get(position)));

            }

            @Override
            public void onTabReselect(int position) {
                refreshPageData();
            }
        });

        tabLayout.setCurrentTab(index);

    }

    private void refreshPageData() {
        EventBus.getDefault().post(new Event<>(Constant.REFRESH_PAGE_DATA_EVENT));
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_home;
    }

    @Override
    protected void onEventProcess(Event event) {

    }
}